package com.skt.common_hr.overtime.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.overtime.model.DeptOtProxy;
	import com.skt.common_hr.overtime.view.components.DeptOTView;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class DeptOtMediator extends CommonMediator
	{
		public static const NAME:String = "DeptOtMediator"; 
		public static const SAVE_OT_COMPLETE:String = "saveOtComplete"; 
		public static const CLOSE_DEPT_OT:String = "closeDeptOt";
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function DeptOtMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			setEventListener();
 		}


		private function setEventListener():void
		{
 			// 각 버튼들의 Event Handler를 설정하고
 			view.addEventListener(DeptOTView.CONFIRM, onConfirm);
 			view.addEventListener(DeptOTView.CANCEL, onCancel);
 			view.addEventListener(DeptOTView.CHANGED_DATE, onChangedDate);
		}


 		public function exec(d:Date, deptCode:String, deptName:String):void
 		{
 			view.selectedDate = d;
 			view.selectedDeptCode = deptCode;
 			view.selectedDeptName = deptName;
 			view.dp = new ArrayCollection();
 			load(d, deptCode);
 			bActivated = true;
 		}
 		
 		
 		private function closeView(cancel:Boolean=false):void
		{
			bActivated = false;
			facade.sendNotification(CLOSE_DEPT_OT, this);
			PopUpManager.removePopUp(view);
		}
 		


		private function load(selectedDate:Date, deptCode:String):void
 		{
 			var proxy:DeptOtProxy = facade.retrieveProxy(DeptOtProxy.NAME) as DeptOtProxy;
 			proxy.load(selectedDate, deptCode);
 		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					DeptOtProxy.LOAD_DEPT_OT_COMPLETE,
					DeptOtProxy.LOAD_DEPT_OT_FAILED,
					DeptOtProxy.SAVE_DEPT_OT_COMPLETE,
					DeptOtProxy.SAVE_DEPT_OT_FAILED
					];
		}
		
		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			if(bActivated) {
				switch(note.getName()) 
				{
					case DeptOtProxy.LOAD_DEPT_OT_COMPLETE:
						view.dp = (note.getBody() as DeptOtProxy).list;
						break;
					case DeptOtProxy.LOAD_DEPT_OT_FAILED:
						break;
					case DeptOtProxy.SAVE_DEPT_OT_COMPLETE:
						//facade.sendNotification(SAVE_OT_COMPLETE, this);
						closeView();
						break;
					case DeptOtProxy.SAVE_DEPT_OT_FAILED:
							var msg:String = "(" + (note.getBody() as DeptOtProxy).retCode
												+ ") " + (note.getBody() as DeptOtProxy).retText;
							CommonAlert.showMessage("", msg, null, "오류");
						break;
				}
			}
		}







 		private function onConfirm(event:Event):void
 		{
 			var proxy:DeptOtProxy = facade.retrieveProxy(DeptOtProxy.NAME) as DeptOtProxy;
 			proxy.save(view.selectedDate, view.selectedDeptCode, getOtList());
 		}
 		
 		

 		private function onCancel(event:Event):void
 		{
 			closeView(true);
 		}
 		
 		private function onChangedDate(e:Event):void
 		{
 			load(view.selectedDate, view.selectedDeptCode);	
 		}
 		
 		private function getOtList():Array 
 		{
 			var arr:Array = new Array;
 			
 			for(var i:int=0; i<view.dp.length; i++) {
 				var vo:Object = new Object();
 				vo.ORGTX = view.dp.getItemAt(i).ORGTX;
 				vo.ORGEH = view.dp.getItemAt(i).ORGEH.toString();
 				vo.OTWOK = view.dp.getItemAt(i).OTWOK.toString();
 				arr.push(vo);
 			}
 			return arr;
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        public function get view():DeptOTView
        {
            return viewComponent as DeptOTView;
        }
	}
}